/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.stock.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.stock.domain.TblStockInfo;
import me.zhengjie.stock.domain.vo.TblStockInfoQueryCriteria;
import me.zhengjie.stock.service.TblStockInfoService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author dolojia
 * @date 2024-05-04
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "股票信息管理")
@RequestMapping("/api/tblStockInfo")
public class TblStockInfoController {

    private final TblStockInfoService tblStockInfoService;

    @Log("导出股票信息数据")
    @ApiOperation("导出股票信息数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('tblStockInfo:list')")
    public void exportTblStockInfo(HttpServletResponse response, TblStockInfoQueryCriteria criteria) throws IOException {
        tblStockInfoService.download(tblStockInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询股票信息")
    @ApiOperation("查询股票信息")
    @PreAuthorize("@el.check('tblStockInfo:list')")
    public ResponseEntity<PageResult<TblStockInfo>> queryTblStockInfo(TblStockInfoQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(tblStockInfoService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增股票信息")
    @ApiOperation("新增股票信息")
    @PreAuthorize("@el.check('tblStockInfo:add')")
    public ResponseEntity<Object> createTblStockInfo(@Validated @RequestBody TblStockInfo resources) {
        tblStockInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改股票信息")
    @ApiOperation("修改股票信息")
    @PreAuthorize("@el.check('tblStockInfo:edit')")
    public ResponseEntity<Object> updateTblStockInfo(@Validated @RequestBody TblStockInfo resources) {
        tblStockInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除股票信息")
    @ApiOperation("删除股票信息")
    @PreAuthorize("@el.check('tblStockInfo:del')")
    public ResponseEntity<Object> deleteTblStockInfo(@RequestBody List<Integer> ids) {
        tblStockInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}